package com.example.test.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.db.Db;
import com.example.test.service.build.UserInfo;
import com.example.test.service.factory.LoginFactory;
import com.example.test.service.zerenlian.*;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.*;


@Service
public class yanzhengma {


    @Autowired
    private DefaultKaptcha kaptcha;

    @Autowired
    private UserInfoBuild userInfoBuild;

    @Autowired
    LoginFactory loggerFactory;

    public void yanzhengma(HttpServletResponse response) {
        String text = kaptcha.createText();
        BufferedImage image = kaptcha.createImage(text);
        try {
            ImageIO.write(image, "jpg", new File("D:\\tt.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(text);

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        System.out.println(lineCaptcha.getCode());
        //将验证码放入session
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public int loginyanzheng(int num, String code) {
        int login1 = loggerFactory.getLogin(code).login(num);
        return login1;
    }


    public int zerenlian(int num) {
        AddBean addBean = new AddOne(new AddTwo(new AddOne(new AddThree(null))));
        return addBean.addNum(num);
    }

    public UserInfo build() throws SQLException {

        Connection connection = Db.use().getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select query_all_dgm_device()");

        int count = resultSet.getMetaData().getColumnCount();
        while (resultSet.next()) {//遍历行，next()方法返回值为Boolean，当存在数据行返回true,反之false
            for (int i = 1; i <= count; i++) {//遍历列
                System.out.print(resultSet.getMetaData().getColumnName(i) + "----------" + resultSet.getString(i));
                if (i < count) {
                    System.out.print("---");//为数据隔开美观，没实际意义
                }
            }
            System.out.println();
        }

        UserInfo userInfo = userInfoBuild.buildBase().build1().build2().build();

        return userInfo;
    }


    public static void main(String[] args) {
        AddBean addBean = new AddOne(new AddTwo(new AddOne(new AddThree(new AddFour(null)))));
        System.out.println(addBean.addNum(1));
    }
}
